package com.zuo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zuo.entity.Department;
import com.zuo.entity.User;
import com.zuo.service.DepartmentService;
import com.zuo.util.HrmConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Author 白白旧维
 * @Date 2022/3/2 17:51
 */
@Controller
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/dept")
    public String department(Model model, @RequestParam(value = "page",defaultValue = "1") int page){
        PageHelper.startPage(page, HrmConstants.PAGE_DEFAULT_SIZE);

        List<Department> departments = departmentService.listDepartment();

        //PageInfo一定要获取到数据库集合之后
        PageInfo<Department> pageInfo = new PageInfo<>(departments);
        int up = page-1;
        int down = page+1;
        int last = pageInfo.getLastPage();
        long total = pageInfo.getTotal();

        model.addAttribute("up",up);//上一页
        model.addAttribute("page",page);//当前页
        model.addAttribute("down",down);//下一页
        model.addAttribute("last",last);//最后一页
        model.addAttribute("total",total);//数据总数
        model.addAttribute("pageSize",HrmConstants.PAGE_DEFAULT_SIZE);//页面大小
        model.addAttribute("departments",departments);
        model.addAttribute("type","all");
        return "dept/dept.jsp";
    }

    @RequestMapping("/searchDept")
    public String searchDepartment(String dname,Model model, @RequestParam(value = "page",defaultValue = "1") int page){
        PageHelper.startPage(page,HrmConstants.PAGE_DEFAULT_SIZE);

        List<Department> departments = departmentService.searchDepartment(dname);

        //PageInfo一定要获取到数据库集合之后
        PageInfo<Department> pageInfo = new PageInfo<>(departments);
        int up = page-1;
        int down = page+1;
        int last = pageInfo.getLastPage();
        long total = pageInfo.getTotal();

        model.addAttribute("up",up);//上一页
        model.addAttribute("page",page);//当前页
        model.addAttribute("down",down);//下一页
        model.addAttribute("last",last);//最后一页
        model.addAttribute("total",total);//数据总数
        model.addAttribute("pageSize",HrmConstants.PAGE_DEFAULT_SIZE);//页面大小
        model.addAttribute("departments",departments);
        model.addAttribute("type","search");
        model.addAttribute("dname",dname);
        return "dept/dept.jsp";
    }

    @RequestMapping("/toUpdateDept")
    public String toUpdateDept(int id,Model model){
        Department department = departmentService.getOne(id);
        model.addAttribute("dept",department);
        return "dept/showUpdateDept.jsp";
    }

    @RequestMapping("/updateDept")
    public String updateDept(Department department,Model model){
        int result = departmentService.updateDepartment(department);
        if(result!=0){
            model.addAttribute("info","成功修改了"+result+"条数据");
        }else{
            model.addAttribute("info","修改失败");
        }
        return "forward:dept.action";
    }

    @RequestMapping("/removeDept")
    public String removeDept(String ids,Model model){
        int result = departmentService.removeDepts(ids);
        model.addAttribute("info","成功删除了"+result+"条记录");
        return "forward:dept.action";
    }

    @RequestMapping("/toAddDept")
    public String toAddDept(){
        return "dept/showAddDept.jsp";
    }

    @RequestMapping("/addDept")
    public String addDept(Department department,Model model){
        int result = departmentService.addDept(department);
        if(result!=0){
            model.addAttribute("info","添加成功");
        }else{
            model.addAttribute("info","添加失败");
        }
        return "forward:dept.action";
    }

}
